package com.arctan.mhxy_utils_android;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.support.multidex.MultiDex;

import androidx.core.math.MathUtils;
import androidx.room.Room;

import com.arctan.mhxy_utils_android.common.Constants;
import com.arctan.mhxy_utils_android.db.AppDataBase;
import com.arctan.mhxy_utils_android.utils.LogUtil;
import com.elvishew.xlog.LogConfiguration;
import com.elvishew.xlog.LogLevel;
import com.elvishew.xlog.XLog;
import com.elvishew.xlog.printer.AndroidPrinter;
import com.elvishew.xlog.printer.ConsolePrinter;
import com.elvishew.xlog.printer.Printer;
import com.elvishew.xlog.printer.file.FilePrinter;
import com.elvishew.xlog.printer.file.backup.NeverBackupStrategy;
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy;

import java.io.File;

public class Session extends Application {
    private static AppDataBase db;
    public static Application  mApplication;
    public static Handler mHandler = new Handler();
    public static Printer logPrinter;
    @Override
    public void onCreate() {
        super.onCreate();
        mApplication=this;
        initLog();
        initDb();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    private void initLog() {
        LogConfiguration configuration = new LogConfiguration.Builder()
                .logLevel(LogLevel.ALL)
                .disableThreadInfo()

                .build();
        Printer androidPrinter = new AndroidPrinter(true);         // Printer that print the log using android.util.Log
        Printer consolePrinter = new ConsolePrinter();
        // Printer that print the log to console using System.out
        logPrinter = new FilePrinter                      // Printer that print(save) the log to file
                .Builder(getExternalCacheDir() + File.separator + Constants.APP_FOLDER)
                .fileNameGenerator(new LogUtil.MyLogNameGenerator())        // Default: ChangelessFileNameGenerator("log")
                .backupStrategy(new NeverBackupStrategy())             // Default: FileSizeBackupStrategy(1024 * 1024)
                .cleanStrategy(new FileLastModifiedCleanStrategy(7L * 24L * 60L * 60L * 1000L))     // Default: NeverCleanStrategy()
                .flattener(new LogUtil.MyFlattener())
                .build();
        XLog.init(                                                 // Initialize XLog
                configuration,                                                // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
                androidPrinter,                                        // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.                 // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
                consolePrinter,
                logPrinter);
    }
    private static void initDb() {
        db= Room.databaseBuilder(mApplication, AppDataBase.class,Constants.DB_NAME)
                .allowMainThreadQueries()
                .build();
    }
    public  static AppDataBase getAppDb(){
        return db;
    }
}
